﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{

    private Rigidbody2D rb;
    private Animator anim;
    private bool isHurt, isGround;
    public Joystick joystick;
    public LayerMask ground;
    public Collider2D coll, disColl;
    public Transform cellCheck, groundCheck;
    public Text cherryTextNum, gemTextNum;
    [Space]
    public float speed, jumpforce;
    [SerializeField]
    private int cherryCount, gemCount, jumpCount;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        isGround = Physics2D.OverlapCircle(groundCheck.position, 0.2f, ground);

        if (!isHurt)
        {
            Movement();
        }
        SwitchAnim();
    }

    void Update()
    {
        Crouch();
        ClickButtonJump();
        cherryTextNum.text = cherryCount.ToString();
        gemTextNum.text = gemCount.ToString();
    }

    void Movement()
    {
        float hMove = joystick == null ? Input.GetAxis("Horizontal") : joystick.Horizontal;

        rb.velocity = new Vector2(hMove * speed * Time.fixedDeltaTime, rb.velocity.y);
        float faceDirection = joystick == null ? Input.GetAxisRaw("Horizontal") : hMove;
        //角色移动
        if (hMove != 0)
        {
            anim.SetFloat("running", Mathf.Abs(hMove));
        }
        else
        {
            anim.SetFloat("running", 0);
        }

        if (joystick == null)
        {
            if (faceDirection != 0)
            {
                transform.localScale = new Vector3(faceDirection, 1, 1);
            }
        }
        else
        {
            if (faceDirection > 0)
            {
                transform.localScale = new Vector3(1, 1, 1);
            }
            else if (faceDirection < 0)
            {
                transform.localScale = new Vector3(-1, 1, 1);
            }

        }

    }

    void SwitchAnim()
    {
        // anim.SetBool("idle", false);

        if (rb.velocity.y < 0.1f && !coll.IsTouchingLayers(ground))
        {
            anim.SetBool("falling", true);
        }

        if (anim.GetBool("jumping"))
        {
            if (rb.velocity.y < 0)
            {
                anim.SetBool("jumping", false);
                anim.SetBool("falling", true);
            }
        }
        else if (isHurt)
        {
            anim.SetBool("hurt", true);
            if (Mathf.Abs(rb.velocity.x) <= 0.1)
            {
                isHurt = false;
                anim.SetBool("hurt", false);
                // anim.SetBool("idle", true);
                anim.SetFloat("running", 0);
            }
        }
        else if (coll.IsTouchingLayers(ground))
        {
            anim.SetBool("falling", false);
            // anim.SetBool("idle", true);
        }
    }

    //吃荔枝
    void OnTriggerEnter2D(Collider2D collider)
    {
        if (collider.tag == "Collection")
        {
            SoundManager.instance.CollectionAudioPlay();
            collider.GetComponent<Animator>().Play("collectionGot");
        }
        else if (collider.tag == "DeadLine")
        {
            SoundManager.instance.BgmControl(false);
            Invoke("Restart", 2f);
        }
    }

    //消灭敌人
    void OnCollisionEnter2D(Collision2D collider)
    {
        if (collider.gameObject.tag == "Enemy")
        {
            if (anim.GetBool("falling"))
            {
                // Destroy(collider.gameObject);
                Enemy enemy = collider.gameObject.GetComponent<Enemy>();
                enemy.JumpOn();
                Jump();
            }
            //受伤
            else if (transform.position.x < collider.transform.position.x)
            {
                SoundManager.instance.HurtAudioPlay();
                isHurt = true;
                rb.velocity = new Vector2(-10, rb.velocity.y);
            }
            else if (transform.position.x > collider.transform.position.x)
            {
                SoundManager.instance.HurtAudioPlay();
                isHurt = true;
                rb.velocity = new Vector2(10, rb.velocity.y);
            }
        }
    }

    void Crouch()
    {
        if (!Physics2D.OverlapCircle(cellCheck.position, 0.2f, ground))
        {
            if (joystick == null ? Input.GetButton("Crouch") : joystick.Vertical < -0.5)
            {
                disColl.enabled = false;
                anim.SetBool("crouching", true);
            }
            else
            {
                disColl.enabled = true;
                anim.SetBool("crouching", false);
            }
        }
    }

    void ClickButtonJump()
    {

        if (isGround)
        {
            jumpCount = 1;
        }

        if ((joystick == null ? Input.GetButtonDown("Jump") : joystick.Vertical > 0.5f) && jumpCount > 0)
        {
            jumpCount--;
            Jump();
        }

        //角色跳跃
        if ((joystick == null ? Input.GetButtonDown("Jump") : joystick.Vertical > 0.5f) && jumpCount == 0 && isGround)
        {
            Jump();
        }
    }

    void Jump()
    {
        SoundManager.instance.JumpAudioPlay();
        rb.velocity = Vector2.up * jumpforce;
        anim.SetBool("jumping", true);
    }



    void Restart()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void CherryPlus()
    {
        cherryCount++;
    }

    public void GemPlus()
    {
        gemCount++;
    }
}
